﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Controls.DataVisualization.Charting;

namespace luanvanFinal
{
    /// <summary>
    /// Interaction logic for wdReqRiskFirstShow.xaml
    /// </summary>
    public partial class wdReqRiskFirstShow : Window
    {
        private int idProject;
        private String idReq;
        public wdReqRiskFirstShow()
        {
            InitializeComponent();
        }
        public int IdProject
        {
            get { return idProject; }
            set { idProject = value; }
        }
        public String IdReq
        {
            get { return idReq; }
            set { idReq = value; }
        }
        private void btnChartCus_Click(object sender, RoutedEventArgs e)
        {
            wdCharCusReq chart = new wdCharCusReq();
            chart.Show();
        }
        public void showToDatagrid()
        {
            int STT = 1;            
            clsReqRisk reqRisk = new clsReqRisk();
            String SQL = "select idReq as ReqTittle,sumCos as Costumer,sumBus as Bussiness, sumImp as Implement,sumQua as Quality,total as Total from reqRisk where idProject='" + idProject + "' order by total DESC ";
            DataTable dt = reqRisk.getDataReqRisk(SQL);
            DataTable getDt = new DataTable();
            getDt.Columns.Add("Num");
            getDt.Columns.Add("Requirement Title");
            getDt.Columns.Add("Costumer");
            getDt.Columns.Add("Bussiness");
            getDt.Columns.Add("Implement");
            getDt.Columns.Add("Quality");
            getDt.Columns.Add("Total");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow dr = getDt.NewRow();
                dr[0] = STT.ToString();
                dr[1] = dt.Rows[i][0].ToString();
                dr[2] = dt.Rows[i][1].ToString();
                dr[3] = dt.Rows[i][2].ToString();
                dr[4] = dt.Rows[i][3].ToString();
                dr[5] = dt.Rows[i][4].ToString();
                dr[6] = dt.Rows[i][5].ToString();
                STT++;
                getDt.Rows.Add(dr);
                getDt.AcceptChanges();
            }
            datagrid.ItemsSource = getDt.DefaultView;
        }

        private void btnChartImp_Click(object sender, RoutedEventArgs e)
        {
            wdChartImpReq chart = new wdChartImpReq();
            chart.Show();
        }

        private void btnChartQua_Click(object sender, RoutedEventArgs e)
        {
            wdChartQuaReq chart = new wdChartQuaReq();
            chart.Show();
        }

        private void btnChartBus_Click(object sender, RoutedEventArgs e)
        {
            wdChartBusReq chart = new wdChartBusReq();
            chart.Show();
        }

        private void datagrid_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            DataRowView row = (DataRowView)datagrid.SelectedItems[0];
            String strIdReq = row[1].ToString();
            String SQL = "select * from reqRisk where idProject = '"+idProject+"' and idReq ='"+strIdReq+"'";
            clsReqRisk reqRisk = new clsReqRisk();
            DataTable dt = reqRisk.getDataReqRisk(SQL);
            if (dt != null)
            {
                cusCos.Text = dt.Rows[0][3].ToString();
                cusFun.Text = dt.Rows[0][4].ToString();
                cusCom.Text = dt.Rows[0][5].ToString();
                cusGen.Text = dt.Rows[0][6].ToString();
                cusDoc.Text = dt.Rows[0][7].ToString();
                busCos.Text = dt.Rows[0][8].ToString();
                busFun.Text = dt.Rows[0][9].ToString();
                busGen.Text = dt.Rows[0][10].ToString();
                busDoc.Text = dt.Rows[0][11].ToString();
                busUnd.Text = dt.Rows[0][12].ToString();
                busExp.Text = dt.Rows[0][13].ToString();
                busDev.Text = dt.Rows[0][14].ToString();
                impGen.Text = dt.Rows[0][15].ToString();
                impDoc.Text = dt.Rows[0][16].ToString();
                impRes.Text = dt.Rows[0][17].ToString();
                impTec.Text = dt.Rows[0][18].ToString();
                impExp.Text = dt.Rows[0][19].ToString();
                impDev.Text = dt.Rows[0][20].ToString();
                quaGen.Text = dt.Rows[0][21].ToString();
                quaDoc.Text = dt.Rows[0][22].ToString();
                quaUnd.Text = dt.Rows[0][23].ToString();
                quaExp.Text = dt.Rows[0][24].ToString();
                quaDev.Text = dt.Rows[0][25].ToString();
                quaCon.Text = dt.Rows[0][26].ToString();
                showColumChart(this.IdProject,strIdReq);
            }

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            showToDatagrid();
        }
        public void showColumChart(int idPro, String idReq)
        {

            String strSQL = "select sumCos,sumBus,sumImp,sumQua, total from reqRisk where idProject = '" + idPro + "' and idReq ='"+idReq+"' ";
            clsConnect ketnoi = new clsConnect();
            ketnoi.openConnection();
            SqlDataAdapter da = new SqlDataAdapter(strSQL, ketnoi.Connection);
            DataTable dt = new DataTable();
            da.Fill(dt);

            ((ColumnSeries)mcChart.Series[0]).ItemsSource =
                new KeyValuePair<string, int>[]{
            new KeyValuePair<string, int>("Costumer", Convert.ToInt32(dt.Rows[dt.Rows.Count-1][0].ToString())),
            new KeyValuePair<string, int>("Bussiness", Convert.ToInt32(dt.Rows[dt.Rows.Count-1][1].ToString())),
            new KeyValuePair<string, int>("Implement", Convert.ToInt32(dt.Rows[dt.Rows.Count-1][2].ToString())),
            new KeyValuePair<string, int>("Quality", Convert.ToInt32(dt.Rows[dt.Rows.Count-1][3].ToString())),
             new KeyValuePair<string, int>("Total", Convert.ToInt32(dt.Rows[dt.Rows.Count-1][4].ToString()))
            };
            ketnoi.closeConnection();
        }
    }
}
